﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：ManagerAlbum.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-03-05 09:48:46
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Common.Extensions.OrderBy;
using DomainModel;
using IDataAccess;
using IManager;

namespace Manager
{

    /// <summary>
    /// 
    /// </summary>
    public class ManagerAlbum : IManagerAlbum
    {

        /// <summary>
        /// 企业相册
        /// </summary>
        public IDataAccessAlbum IDAAlbum { get; set; }


        /// <summary>
        /// 
        /// </summary>
        public ManagerAlbum()
        {
            // TODO:
        }


        /// <summary>
        /// 通过albumId获取
        /// </summary>
        /// <param name="albumId">albumId</param>
        /// <returns>AlbumInfo</returns>
        public AlbumInfo GetAlbum(int albumId)
        {
            IList<string> list = new List<string>();
            list.Add("Company");

            Expression<Func<AlbumInfo, bool>> where = e => e.AlbumId == albumId;
            return IDAAlbum.Find(where, list);
        }


        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="albumInfo">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool AddAlbum(AlbumInfo albumInfo)
        {
            var res = IDAAlbum.Create(albumInfo);
            IDAAlbum.UnitOfWork.Commit();
            if (res != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="albumIdList">Id列表</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeleteAlbum(IList<int> albumIdList)
        {
            try
            {
                foreach (var item in albumIdList)
                {
                    IDAAlbum.Delete(item);
                }

                IDAAlbum.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="albumInfo">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool UpdateAlbum(AlbumInfo albumInfo)
        {
            try
            {
                IDAAlbum.Update(albumInfo);
                IDAAlbum.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<AlbumInfo> LoadAll(Expression<Func<AlbumInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<AlbumInfo>[] orderByExpressions)
        {
            return IDAAlbum.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadCount(Expression<Func<AlbumInfo, bool>> where)
        {
            return IDAAlbum.Count(where);
        }
         
    }

}